var time='';
var param='';
var current=1;
var jName = getCookie('Jname');
//我的权限数组
var arrayTitle = new Array; 
findMyCatalogue();
loadMyEssay(time, param);
//countPeopleMoney(gmtDatetime, name, phone,orderStatus);
var status = getvl("status");
var payType = getvl("payType");

$(function() {
	showOrHide();	
});


function loadMyEssay(time, param) {
	$(document).ready(function() {
		init(current);
	});
	function init(pageNo) {
		$("#thislist").html("");
		var giveStatus = $("#selectBox option:selected").val();
		$.ajax({
			url: urlcore + "/api/orders/selectListByPageAdmin?time=" + time + "&param=" + param +  "&current=" + pageNo+"&status=" + status+"&payType=" + payType+"&giveStatus=" + giveStatus,
			type: "get",
			async: 'false',
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.records, function(i, n) {
						var id = n.id;
						var adminName;
						if(n.passAdmin==null){
							adminName = '暂无';	
						}else{
							adminName = n.passAdmin.userName;
						}
						var thislist =
							'<tr class="footable-even" style="display: table-row;">' +
							'<td class="footable-visible">' + n.id + '</td>' +
							'<td class="footable-visible">' + n.orderNumber + '</td>' +
							'<td class="footable-visible">' + n.user.realName + '</td>' +
							'<td class="footable-visible">' + n.user.phone + '</td>' +
							'<td class="footable-visible">' + n.orderPrice + '</td>' +
							'<td class="footable-visible">' + n.realMakeMoney+'</td>' +	
							'<td class="footable-visible">' + n.wasteMoney+'</td>' +	
							'<td class="footable-visible">' + n.needPayMoney + '</td>' +
							'<td class="footable-visible">' + n.limitDays + '</td>' +
							'<td class="footable-visible">' + formatDate(n.overdueTime,"yyyy-MM-dd") + '</td>' +
							'<td class="footable-visible">' + formatDate(n.creatTime,"yyyy-MM-dd HH:mm:ss")+'</td>';
							if(n.status==2 || n.status== 4 || n.status== 5 || n.status== 6 || n.status== 7 || n.status==8 || n.status==9){
								thislist += '<td class="footable-visible">' + formatDate(n.refundTime,"yyyy-MM-dd HH:mm:ss")  +'</td>';
							}
							if(n.status== 4 || n.status== 5 || n.status== 6 || n.status== 7 || n.status==8 || n.status==9){
								thislist += '<td class="footable-visible">' + formatDate(n.passTime,"yyyy-MM-dd HH:mm:ss")  +'</td>';
							}
							if(n.status== 5 || n.status== 6 || n.status== 7  || n.status==9){
								thislist += '<td class="footable-visible">' + formatDate(n.giveTime,"yyyy-MM-dd HH:mm:ss")  +'</td>';
							}
							if(n.status== 7){
								thislist += '<td class="footable-visible">' + formatDate(n.finishTime,"yyyy-MM-dd HH:mm:ss")  +'</td>';
							}
							if(n.status== 6 || n.status==9){
								thislist += '<td class="footable-visible">' + n.overdueDays  +'</td>' +
											'<td class="footable-visible">' + n.overdueMoney  +'</td>';
							}
							thislist += 
							'<td class="footable-visible">' + adminName + '</td>' +
							'<td class="footable-visible footable-last-column">'+
								'<a hidden="hidden" class="" name="查看认证信息" href="personMessage.html?userId='+n.userId+'"  >查看认证信息</a>&nbsp;'+
								'<a hidden="hidden" class="" name="运营商报告" onclick="getReport('+n.userId+')" >运营商报告</a>&nbsp;'+
								'<a hidden="" class="btn btn-primary btn-xs"  href="peopleMsg.html?userId=' + n.userId + '" >云慧眼画像</a>&nbsp;'+
								'<a hidden="hidden" class="" name="通讯录" href="user_phone_list.html?id=' + n.userId + '" >通讯录</a>&nbsp;'+
								'<a hidden="hidden" class="" name="详情" href="cash_stripes_order_detail.html?id=' + n.id + '" >详情</a>&nbsp;';
								if(n.status==2){
									thislist += '<a hidden="hidden" name="同意" class="" href="javascript:;"  onclick="thisAgree(' + id + ')">同意</a>&nbsp;'+
												'<a hidden="hidden" name="拒绝" class="" href="javascript:;"  onclick="thisRefuse(' + id + ')">拒绝</a>&nbsp;';
								}
								if(n.status==6){
									thislist += '<a hidden="hidden" name="拉入坏账" class="" href="javascript:;"  onclick="badOrder(' + id + ')">拉入坏账</a>&nbsp;';
								}
								if(n.status==4){
									if(n.giveStatus==null){
										thislist += '<a hidden="hidden" name="打款" class="" href="javascript:;"  onclick="payOrder(' + id + ')">打款</a>&nbsp;';
									}
								}
								thislist += '</td>'+			
							'</tr>';
					
						$('#thislist').append(thislist);

					});
					$.each(arrayTitle, function(i,k) {
						$('a[name="'+k+'"]').attr("hidden",false).attr("class","btn btn-primary btn-xs");
					});
					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});
				
					if(data.code == 'OVERTIME') {
						var thisUrl = window.location.href;

						if(thisUrl.indexOf('login.html') <= -1) {
							top.window.location.href = "login.html";
						}

					} else {
						if(data.msg != '空数据') {
							//alert(data.msg)
						} else {
							$('#thiscount').text(0);
						}
					}
				}

			},
			error: function() {
				alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}

}

function EnterSearchList() {
	var code = event.keyCode;
	if(code == 13) {
		searchList();
	}

}

function searchList() {
	var param = $('#param').val().trim();
	var time = $('#applyTime').val().trim();
	loadMyEssay(time, param);
	//countPeopleMoney(time, userName, phoneNumber,'');

}





function findMyCatalogue(){
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle="+jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				arrayTitle.push(n.thirdCatalogue.title);
			});
		} else {
			alert(data.msg);
		}

		},
		error:function() {
			alert("error");
		}
	});
}

/**
 * 时间格式化
 */
function formatDate(dateTime,format){
    return null==dateTime?"":new Date(dateTime).pattern(format);
}


function passCashOrder(id) {
	if(confirm("您确定发货？")) {
		$.ajax({
			url: urlcore + "/api/orders/passCashOrder?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				console.log(data.msg);
				alert(data.msg);
			}
		});
	}


}

function confirmCashOrder(id) {
	if(confirm("您确定已经收货？")) {
		$.ajax({
			url: urlcore + "/api/orders/confirmCashOrder?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				location.reload()
			},
			error: function() {
				/* Act on the event */
				console.log(data.msg);
				alert(data.msg);
			}
		});
	}


}

function thisAgree(id) {
	$.ajax({
		url: urlcore + "/api/orders/agreeOrder?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			location.reload()
		},
		error: function() {
			/* Act on the event */
			console.log(data.msg);
			alert(data.msg);
		}
	});
}

function thisRefuse(id) {
	$.ajax({
		url: urlcore + "/api/orders/refuseOrder?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			location.reload()
		},
		error: function() {
			/* Act on the event */
			console.log(data.msg);
			alert(data.msg);
		}
	});
}

function badOrder(id) {
	$.ajax({
		url: urlcore + "/api/orders/badOrder?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			location.reload()
		},
		error: function() {
			/* Act on the event */
			console.log(data.msg);
			alert(data.msg);
		}
	});
}


function payOrder(id) {
	$.ajax({
		url: urlcore + "/api/user/payOrder?orderId=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			location.reload()
		},
		error: function() {
			/* Act on the event */
			alert(data.msg);
		}
	});
}

function getReport(userId) {
	$.ajax({
		url: urlcore + "/api/userPhone/getReport?userId=" + userId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data){
			window.open("https://tenant.51datakey.com/carrier/report_data?data="+data.data.message);   
		},
		error: function() {
			/* Act on the event */
			alert(data.msg);
		}
	});
}



/**
 * 表格显示隐藏
 */
function showOrHide(){
    if(status==1){
		$("#a").attr("style","display:none;");
		$("#b").attr("style","display:none;");
		$("#c").attr("style","display:none;");
		$("#d").attr("style","display:none;");
		$("#e").attr("style","display:none;");
		$("#f").attr("style","display:none;");
	}else if(status==2 ||status==8){
		$("#a").attr("style","visibility:visible;");
		$("#b").attr("style","display:none;");
		$("#c").attr("style","display:none;");
		$("#d").attr("style","display:none;");
		$("#e").attr("style","display:none;");
		$("#f").attr("style","display:none;");
	}else if(status==4){
		$("#a").attr("style","visibility:visible;");
		$("#b").attr("style","visibility:visible;");
		$("#c").attr("style","display:none;");
		$("#d").attr("style","display:none;");
		$("#e").attr("style","display:none;");
		$("#f").attr("style","display:none;");
		$("#selectBox").attr("style","display:block;");
	}else if(status==5){
		$("#a").attr("style","visibility:visible;");
		$("#b").attr("style","visibility:visible;");
		$("#c").attr("style","visibility:visible;");
		$("#d").attr("style","display:none;");
		$("#e").attr("style","display:none;");
		$("#f").attr("style","display:none;");
	}else if(status==6 || status==9){
		$("#a").attr("style","visibility:visible;");
		$("#b").attr("style","visibility:visible;");
		$("#c").attr("style","visibility:visible;");
		$("#d").attr("style","display:none;");
		$("#e").attr("style","visibility:visible;");
		$("#f").attr("style","visibility:visible;");
	}else if(status==7){
		$("#a").attr("style","visibility:visible;");
		$("#b").attr("style","visibility:visible;");
		$("#c").attr("style","visibility:visible;");
		$("#d").attr("style","visibility:visible;");
		$("#e").attr("style","display:none;");
		$("#f").attr("style","display:none;");	
	}
}
